#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef WIN32
#include <unistd.h>
#endif

#include <dicom/dicom.h>
#include "dicop.h"

struct PersonInfo {
    char name[32];
    char age[8];
    char height[8];
    char weight[8];
} Person;

callback_t setPersonInfo(callback_args_t args)
{
    dicop_callback_get_args(args, 4, "name", Person.name, "age", Person.age, "height", Person.height, "weight", Person.weight);

    return NULL;
}

callback_t getPersonInfo(callback_args_t args)
{
    char name[32] = { '\0' };

    dicop_callback_get_args(args, 1, "name", name);
    if (strncmp(name, Person.name, strlen(Person.name)) == 0)
        return dicop_callback_put_reply(3, "age", Person.age, "height", Person.height, "weight", Person.weight);

    /* NOTE: Must return an empty list_head rather than a null pointer */
    return dicop_callback_null_reply();
}

int main(void)
{
    struct dicop_app *ContactApp;
    struct dicop_method_desc *MethodSetPersonInfo, *MethodGetPersonInfo;
    struct dicop_signal_desc *SignalChange, *SignalTerminate;

    bzero(&Person, sizeof(Person));

    dicop_init();

    ContactApp = dicop_app_new("Contact");

    MethodSetPersonInfo = dicop_method_new(ContactApp, "setPersonInfo");
    MethodGetPersonInfo = dicop_method_new(ContactApp, "getPersonInfo");

    dicop_method_callback_install(MethodSetPersonInfo, setPersonInfo);
    dicop_method_callback_install(MethodGetPersonInfo, getPersonInfo);

    dicop_method_args_init(MethodSetPersonInfo, 4, "name", "age", "height", "weight");
    dicop_method_args_init(MethodGetPersonInfo, 1, "name");
    dicop_method_reply_init(MethodGetPersonInfo, 3, "age", "height", "weight");

    SignalChange = dicop_signal_new(ContactApp, "Change");
    dicop_signal_args_init(SignalChange, 1, "name");

    SignalTerminate = dicop_signal_new(ContactApp, "Terminate");

    dicop_server_create(12250);

    printf("Get ready to emit signals?");
    getchar();
    dicop_signal_emit(SignalChange, "name", "Chardi.Chang");
    getchar();
    dicop_signal_emit(SignalTerminate);

    dicop_server_terminate();

    return 0;
}

